// search.js
var fs = require('fs');
var path = require('path');
/*路由*/
const router = require('koa-router')();
/*接口服务*/
const util = require('../utilitys');
/*配置属性*/

const dir = '../../svg/';

// 获取svg文件夹接口
router.get(`/typeList`, async (ctx) => {
  // console.log(ctx);

  try {
    let result = {};
    result.list = await typeListFn(ctx);
    if (result) {
      ctx.body = util.resultSuccessJson(undefined, '成功', result);
    } else {
      ctx.body = util.resultSuccessJson(undefined, '没找到', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
  console.log('ctx');
});
// 搜索svg接口
router.get(`/search`, async (ctx) => {
  console.log(ctx);
  // type 土建 安装
  let type =
    decodeURIComponent(util.getQueryVariable('type', ctx.request.url)) || '';
  let sk =
    decodeURIComponent(util.getQueryVariable('sk', ctx.request.url)) || '';

  console.log('sk', sk);
  console.log('type', type);
  try {
    // let result = await searchDao.search(ctx, { sk });
    let result = {};
    result.list = await searchFn(ctx, { sk, type });
    result.sk = sk;
    result.type = type;

    if (result) {
      ctx.body = util.resultSuccessJson(undefined, '成功', result);
    } else {
      ctx.body = util.resultSuccessJson(undefined, '没找到', {});
    }
  } catch (err) {
    ctx.body = util.resultErrorJson(undefined, err, {});
  }
});
// 文件夹列表
async function typeListFn() {
  let data = [];
  // let dir = `./svg/`;
  console.log('dir', path.join(__dirname, dir));
  const pullDir = path.join(__dirname, dir);
  var fsDir = fs.readdirSync(pullDir);
  console.log('fsDir', fsDir);

  for (let i = 0; i < fsDir.length; i++) {
    const file = fsDir[i];
    console.log('file', file);
    var pathname = path.join(__dirname, dir, file);
    console.log('pathname', pathname);
    if (fs.statSync(pathname).isDirectory() && pathname.indexOf('design') < 0) {
      data.push(file);
    }
  }
  // .forEach(function(file) {

  // });
  return data;
}
async function travel(dir, obj, level) {
  var data = [];
  var fsDir = fs.readdirSync(dir);
  for (let i = 0; i < fsDir.length; i++) {
    const file = fsDir[i];
    // async function(file) {
    var pathname = path.join(dir, file);
    if (pathname.indexOf('design') > -1 && level === 0) {
      continue
    }

    if (fs.statSync(pathname).isDirectory()) {
      console.log('pathname', pathname);
      let data2 = await travel(pathname, obj, level++);
      data = data.concat(data2);
      console.log('data2', data2.length);
      console.log('data', data.length);
    } else if (file != '.DS_Store') {
      // console.log('da', pathname);

      if (pathname.indexOf(obj.sk) > -1 || obj.sk === '') {

        data.push(pathname.replace(/\\/g,'/').replace(/^.+?\/svg\//, 'svg/'));
        // return data;
      }
      // data = callback(pathname.split('/svg/')[1]);
    } else {
      console.log('file', file);
    }
    // }
  }
  // await .forEach();
  console.log('data---', data);

  return data;
}
async function searchFn(ctx, obj) {
  let data = [];
  // let dir = `./svg/`;
  let newdir = obj.type ? dir + obj.type + '/' : dir;

  const pullDir = path.join(__dirname, newdir);
  console.log('pullDir', pullDir);

  data = await travel(pullDir, obj, 0);

  return data;
}
// let d = [1];
// d = d.concat([2]);
// console.log('d', d);

module.exports = router;
